/**
 * @class yijs.proxy.MicroBlog
 * @fileOverview 微博远程代理
 * @author 谢文浩
 * @email mohaiguyan12@126.com
 * @version 0.1
 * @date 2010-02-25
 */
yijs.proxy = yijs.proxy || {};
/**
 * @constructor
 * @param {Object} options
 */
yijs.proxy.MicroBlog = function(){

}
yijs.proxy.MicroBlog.prototype = {
	authorization : null,
	count : 20,
	type : "xml",
	TYPES : ["json","xml","rss","atom"],
    URL : null,
	URIS : ['search','statuses','users','friendships','direct_messages','account','tag','register','help','favorites','blocks'],
	ACTIONS : {
		/**搜索*/
		search : "search",
		/**显示大家在做啥*/
		publicMsg : "public_timeline",
		/**显示用户在做啥*/
		userMsg : "user_timeline",
		/**获取我和好友的消息*/
		friendsMsg : "friends_timeline",
		/**获取一条指定的消息*/
		getMsg : "show",
		/**发布一条新消息*/
		updateMsg : "update",
		/**删除一条消息*/
		delMsg : "destroy",
		/**获取一条指定的消息*/
		getMentions : "mentions",
		/**用户验证*/
		verify : "verify",
		user : "show",
		/**获取朋友列表*/
		friends : "friends",
		/**获取朋友统计数*/
		friendsCount : "friends_count",
		/**获取粉丝列表*/
		followers : "followers"				
	},
	Preload : null,
	/**
	 * 触发ajax向服务器端请求数据
	 * @param {Object} options 参数
	 */
	ajax : function(options){
		var _that = this;
		var _successMsgDelay = 800;
		var _completeMsgDelay = 4000;
		var data =  options.data || {};
		data.count = this.count;
		$.ajax({
		    url : options.url,
		    method : options.method || 'GET',
			dataType : options.dataType || 'xml',
			data : data,
		    beforeSend : function(req) {
		        req.setRequestHeader('Authorization', _that.authorization);
				if(_that.Preload != null && options.beforeSendMsg)_that.Preload.show(options.beforeSendMsg);
		    },
			success : function(data){
				if (_that.Preload != null) {
					options.beforeSendMsg && _that.Preload.msg(options.successMsg);
					_that.Preload.hide(_successMsgDelay,function(){
						if(options.success) options.success.call(this,data);
					});						
				}else{
					if(options.success) options.success.call(this,data);
				}
			},
			complete : function(){
				/*
				if (_that.Preload != null) {
					_that.Preload.hide(_completeMsgDelay,function(){
						if(options.complete) options.complete.call(this);
					});					
				}else{
					if(options.complete) options.complete.call(this);
				}
				*/

			}
		});			
	},
	/**
	 * 获取API
	 * @param {Object} uriIndex uri数组索引值（第二段）
	 * @param {Object} actionKey action对象key值（第三段）
	 * @param {Object} param	参数（第四段）
	 * @return {String} API
	 */
	getApi : function(uriIndex,actionKey,param){
		if(this.URL != null){
			var _url = this.URL+this.URIS[uriIndex]+"/"+this.ACTIONS[actionKey];
			if(param) _url += "/"+param;
			_url += "."+this.type;
			return _url;
		}
		return null;
	},
	/**用户验证*/
	verify : function(success){
		var _that = this;
		var url = this.getApi(5,"verify");
		this.ajax({
			url : url,
		    success : function(data){
		  		if(success) success.call(this,data);
		    }			
		})
	},
	/**
	 * 获取用户信息
	 * @param {Function} success	 成功获取数据后执行的回调函数
	 * @param {String} beforeSendMsg 触发ajax前显示的信息
	 * @param {String} successMsg	 成功获取后显示的信息
	 * @param {String} param		 参数
	 */
	getUser : function(success,beforeSendMsg,successMsg,param){
		var _that = this;
		var url = this.getApi(2,"user",param);
		this.ajax({
			url : url,
		    success : function(data){
		  		if(success) success.call(this,data);
		    },
			beforeSendMsg : beforeSendMsg || null,
			successMsg : successMsg || null							
		})		
	},
	/**
	 * 获取广场消息
	 * @param {Function} success	 成功获取数据后执行的回调函数
	 * @param {String} beforeSendMsg 触发ajax前显示的信息
	 * @param {String} successMsg	 成功获取后显示的信息
	 */
	getPublicMsg : function(success,beforeSendMsg,successMsg){
		var url = this.getApi(1,"publicMsg");
		this.ajax({
			url : url,
		    success : function(data){
		  		if(success) success.call(this,data);
		    },
			beforeSendMsg : beforeSendMsg || null,
			successMsg : successMsg || null				
		})		
	},
	/**
	 * 获取用户的消息
	 * @param {Function} success	 成功获取数据后执行的回调函数
	 * @param {String} beforeSendMsg 触发ajax前显示的信息
	 * @param {String} successMsg	 成功获取后显示的信息
	 */
	getUserMsg : function(success,beforeSendMsg,successMsg){
		var url = this.getApi(1,"userMsg");
		this.ajax({
			url : url,
		    success : function(data){
		  		if(success) success.call(this,data);
		    },
			beforeSendMsg : beforeSendMsg || null,
			successMsg : successMsg || null			
		})		
	},
	/**
	 * 获取我和好友的消息
	 * @param {Function} success	 成功获取数据后执行的回调函数
	 * @param {String} beforeSendMsg 触发ajax前显示的信息
	 * @param {String} successMsg	 成功获取后显示的信息
	 */
	getFriendsMsg : function(success,beforeSendMsg,successMsg){
		var url = this.getApi(1,"friendsMsg");
		this.ajax({
			url : url,
		    success : function(data){
		  		if(success) success.call(this,data);
		    },
			beforeSendMsg : beforeSendMsg || null,
			successMsg : successMsg || null			
		})			
	},
	/**
	 * 获取指定用户的朋友列表
	 */
	getFriends : function(success,beforeSendMsg,successMsg,page){
		var url = this.getApi(1,"friends");
		this.ajax({
			url : url,
		    success : function(data){
		  		if(success) success.call(this,data);
		    },
			data : {"page" : page || 1},
			beforeSendMsg : beforeSendMsg || null,
			successMsg : successMsg || null			
		})			
	},
	/**
	 * 获取朋友数统计
	 * @param {Object} success
	 * @param {Object} username
	 */
	getFriendsCount : function(success,username){
	    var url = this.getApi(1,"friendsCount");
		this.ajax({
			url : url,
		    success : function(data){
		  		if(success) success.call(this,data);
		    }		
		})		
	},
	/**
	 * 获取指定用户的粉丝列表
	 */
	getFollowers : function(success,beforeSendMsg,successMsg){
		var url = this.getApi(1,"followers");
		this.ajax({
			url : url,
		    success : function(data){
		  		if(success) success.call(this,data);
		    },
			beforeSendMsg : beforeSendMsg || null,
			successMsg : successMsg || null			
		})			
	},	
	getMsg : function(){
		
	},
	updateMsg : function(){
		
	},
	delMsg : function(){
		
	}		
}
